//#region 时间
let spnyr = document.getElementById('spnyr');
let spsfm = document.getElementById('spsfm');

function getTime() {
  let nyr = new Date();
  let year = nyr.getFullYear();
  let month = nyr.getMonth() + 1;
  let date = nyr.getDate();
  date = date < 10 ? '0' + date : date;
  return `${year}年${month}月${date}日`;
}

spnyr.innerHTML = getTime();

setInterval(() => {
  spnyr.innerHTML = getTime();
}, 1000);

function getNowTime() {
  let sfm = new Date();
  let hour = sfm.getHours();
  let minute = sfm.getMinutes();
  let second = sfm.getSeconds();

  hour = hour < 10 ? '0' + hour : hour;
  minute = minute < 10 ? '0' + minute : minute;
  second = second < 10 ? '0' + second : second;
  return `${hour}:${minute}:${second}`;
}

spsfm.innerHTML = getNowTime();

setInterval(() => {
  spsfm.innerHTML = getNowTime();
}, 1000);

//#endregion

//#region 语言课程联动
let selyy = document.getElementById('selyy');

function queryyy() {
  ajax.get('/link/languages', {}, (info) => {
    if (!info.success) {
      alert(info.message);
      return;
    }
    for (let i = 0; i < info.data.length; i++) {
      let yy = info.data[i];
      let op = document.createElement('option');
      op.setAttribute('value', yy.plid);
      op.append(yy.lname);
      selyy.append(op);
    }
  });
}

let selkm = document.getElementById('selkm');

selyy.addEventListener('change', () => {
  // 清除原有的值
  selkm.innerHTML = '';
  // 获取语言的值
  let plid = selyy.value;
  ajax.get(`/link/cource/${plid}`, {}, (info) => {
    if (!info.success) {
      alert(info.message);
      return;
    }
    for (let i = 0; i < info.data.length; i++) {
      let km = info.data[i];
      let op = document.createElement('option');
      op.setAttribute('value', km.lcid);
      op.append(km.cname);
      selkm.append(op);
    }
  });
});

queryyy();

//#endregion
